package com.jltx.xq.relaxnewspro.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jltx.xq.relaxnewspro.R;
import com.jltx.xq.relaxnewspro.database.collection.ICache;
import com.jltx.xq.relaxnewspro.utils.CONSTANT;
import com.jltx.xq.relaxnewspro.utils.HttpUtil;
import com.jltx.xq.relaxnewspro.utils.Settings;
import com.jltx.xq.relaxnewspro.utils.XqLog;


public abstract class BaseListFragment extends Fragment {
    protected int mLayout = 0;
    protected ICache cache;
    protected boolean needCache = true;
    protected RecyclerView.Adapter adapter;
    protected ImageView placeHolder;
    protected ProgressBar progressBar;
//    protected PullToRefreshView refreshView;
    protected boolean withHeaderTab = true;
    protected boolean withRefreshView = true;
    protected RecyclerView recyclerView;
    protected View parentView;
    protected TextView textView;
    protected int pos;
    protected abstract RecyclerView.Adapter bindAdapter();
    protected abstract void onCreateCache();
    protected abstract void loadFromCache();
    protected abstract void loadFromNet();
    protected abstract boolean hasData();
    protected abstract void getArgs();
    protected RecyclerView.LayoutManager mLayoutManager;
    protected SwipeRefreshLayout swipeRefreshLayout;


    public BaseListFragment() {
//         Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setLayout();
        getArgs();
        withHeaderTab = setHeaderTab();
        withRefreshView = setRefreshView();
        parentView = inflater.inflate(mLayout, container, false);
//        textView = (TextView)parentView.findViewById(R.id.tv);
//        textView.setText("text" + pos);

        View view = getActivity().findViewById(R.id.tab_layout);
        if(withHeaderTab){
            view.setVisibility(View.VISIBLE);
        }else{
            if(view !=null) {
                view.setVisibility(View.GONE);
            }
        }

       progressBar = (ProgressBar) parentView.findViewById(R.id.progressbar);
        recyclerView = (RecyclerView) parentView.findViewById(R.id.recyclerView);
        placeHolder = (ImageView) parentView.findViewById(R.id.placeholder);

        onCreateCache();

        adapter = bindAdapter();

        mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(mLayoutManager);

        swipeRefreshLayout = (SwipeRefreshLayout)parentView.findViewById(R.id.swipe_refresh);
        if(withRefreshView){
            swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_orange_dark,android.R.color.holo_green_light,android.R.color.holo_orange_light,android.R.color.holo_red_light);
            swipeRefreshLayout.setDistanceToTriggerSync(400);
            swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    loadFromNet();
                }
            });

            placeHolder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    placeHolder.setVisibility(View.GONE);
                    loadFromNet();
                }
            });
        }else {
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    swipeRefreshLayout.setRefreshing(false);
                }
            });
        }

//        HttpUtil.readNetworkState();
        progressBar.setVisibility(View.VISIBLE);

        new Thread(new Runnable() {
            @Override
            public void run() {
                loadFromCache();
                Log.d("TTTTT","子线程"+Thread.currentThread());
            }
        }).start();
        return parentView;
    }



    protected Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case CONSTANT.ID_FAILURE:
                    if(isAdded()){
                        XqLog.d(getString(R.string.Text_Net_Exception));
                    }
                    break;
                case CONSTANT.ID_SUCCESS:
                    if(isAdded()){
                        XqLog.d(getString(R.string.text_refresh_success));
                    }
                    if(needCache){
                        cache.cache();
                    }
                    break;

                case CONSTANT.ID_FROM_CACHE:
                    if (withRefreshView && hasData() == false) {
                        loadFromNet();
                        return false;
                    } else if (Settings.isAutoRefresh && HttpUtil.isWIFI) {
                        loadFromNet();
                        return false;
                    }
                    break;

            }
            if(withRefreshView){
                swipeRefreshLayout.setRefreshing(false);
            }
            if (hasData()) {
                placeHolder.setVisibility(View.GONE);
            } else {
                placeHolder.setVisibility(View.VISIBLE);
            }
            progressBar.setVisibility(View.GONE);
            adapter.notifyDataSetChanged();
            return false;
        }
    });

    protected boolean setHeaderTab(){return true;}
    protected boolean setRefreshView(){return true;}
    protected boolean setCache(){return true;}
    protected void setLayout(){
        mLayout = R.layout.layout_common_list;
    }

}
